package cn.springBoot.util;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

/**
 * @author ljx
 * @description: TODO
 * @class cn.springBoot.util.PdfUtil
 * @create date: 2021/12/6
 */
public class PdfUtil {

    public void exportPDF(HttpServletRequest request, HttpServletResponse response, String title, String text) {
        Document document = new Document();

        try {
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition",
                "attachment;filename=" + new String((title + ".pdf").getBytes(), "iso-8859-1"));

            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            document.open();
            // 在下面，body中设置了style，设置了默认字体为宋体，这样导出时的html语言就默认带有了字体，汉字才会导出成功
            String content = "<html><body style=\"font-family: 宋体, SimHei;\">"
                + "<p style=\"text-align: center;\"><span style=\"font-family: 黑体, SimHei; font-size: 24px;\">" + title
                + "</span></p>" + text + "</body></html>";
            // 这里是必须要设置编码的，不然导出中文就会乱码。
            byte b[] = content.getBytes("utf-8");
            // 将字节数组包装到流中
            ByteArrayInputStream bais = new ByteArrayInputStream(b);

            XMLWorkerHelper.getInstance().parseXHtml(writer, document, bais, Charset.forName("UTF-8"));

            bais.close();
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
